import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_details_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';
import 'package:xiongan_app_yy/widgets/comm/text_card_widget.dart';
import 'package:xiongan_app_yy/widgets/comm/text_card_widget_new.dart';
import 'package:xiongan_app_yy/widgets/comm/text_exhibition_card_widget.dart';

//信息-出库任务
class OutTaskDetails extends StatefulWidget {
  final String args;

  @override
  State<StatefulWidget> createState() {
    return OutTaskDetailsState();
  }

  OutTaskDetails(this.args);
}

class OutTaskDetailsState extends State<OutTaskDetails> {
  String path;
  var _futureBuilderFuture;
  String data;
  InvListDetailsEntity entity;

  @override
  void initState() {
    super.initState();
    String id = widget.args;
    path = "OMSWarehouseOutTask/$id";
    _futureBuilderFuture = getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "详情",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: FutureBuilder(
        builder: _buildFuture,
        future: _futureBuilderFuture,
      ),
    );
  }

  ///snapshot就是_calculation在时间轴上执行过程的状态快照
  Widget _buildFuture(BuildContext context, AsyncSnapshot snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.none:
        print('还没有开始网络请求');
        return Text('还没有开始网络请求');
      case ConnectionState.active:
        print('active');
        return Text('ConnectionState.active');
      case ConnectionState.waiting:
        print('waiting');
        return Center(
          child: CircularProgressIndicator(),
        );
      case ConnectionState.done:
        print('done');
        if (snapshot.hasError) {
          return Text('Error: ${snapshot.error}');
        }
        return _createListView(context, snapshot);
      default:
        return null;
    }
  }

  Widget _createListView(BuildContext context, AsyncSnapshot snapshot) {
    data = snapshot.data.toString();
    Map<String, dynamic> dataMap = jsonDecode(data);
    if (dataMap["WarehouseOutTask"]["flag"] == "success") {
      entity =
          InvListDetailsEntity.fromJson(dataMap["WarehouseOutTask"]["datas"]);
    }
    return Scrollbar(
        child: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Column(
              children: <Widget>[
                TextExhibitionCardWidget(
                    distance: EdgeInsets.only(
                        right: 15, left: 15, bottom: 16, top: 16),
                    title: "出库任务单号",
                    exhibitionText: entity.datas.taskno),
                TextExhibitionCardWidget(
                    distance: EdgeInsets.only(
                        right: 15, left: 15, bottom: 16, top: 16),
                    title: "领料单号",
                    exhibitionText: entity.datas.erpno),
                TextExhibitionCardWidget(
                    distance: EdgeInsets.only(
                        right: 15, left: 15, bottom: 16, top: 16),
                    title: "供应商",
                    exhibitionText: entity.datas.supplyname ?? ""),
                TextExhibitionCardWidget(
                    distance: EdgeInsets.only(
                        right: 15, left: 15, bottom: 16, top: 16),
                    title: "工程编号",
                    exhibitionText: entity.datas.projectno),
                TextExhibitionCardWidget(
                    distance: EdgeInsets.only(
                        right: 15, left: 15, bottom: 16, top: 16),
                    title: "工程名称",
                    exhibitionText: entity.datas.projectname),
                TextExhibitionCardWidget(
                    distance: EdgeInsets.only(
                        right: 15, left: 15, bottom: 16, top: 16),
                    title: "物资件数",
                    exhibitionText: entity.datas.total.toString()),
                TextExhibitionCardWidget(
                    distance: EdgeInsets.only(
                        right: 15, left: 15, bottom: 16, top: 16),
                    title: "总价",
                    exhibitionText: entity.datas.totalamount.toString()),
                TextCardWidgetNew(
                    margin: EdgeInsets.only(top: 1),
                    onPressed: () {
                      if (entity.datas.carList.length > 0) {
                        Navigator.pushNamed(context, "/carDetails",
                            arguments: {"datas": entity.datas.carList});
                      } else {
                        TostUtil.showToast("暂无车辆信息");
                      }
                    },
                    title: "车辆信息:",
                    textNum: false,
                    details: false,
                    num: "${entity.datas.carList.length}辆",
                    exhibitionTextColor: Color(0xFF999999),
                    border: Border(
                        top: BorderSide(width: 1, color: Color(0xffdddddd)))),
                TextExhibitionCardWidget(
                    distance: EdgeInsets.only(
                        right: 15, left: 15, bottom: 16, top: 16),
                    title: "预约时间",
                    exhibitionText: entity.datas.applytime),
                TextCardWidget(
                    onPressed: () {
                      //Navigator.pushNamed(context, "/storageDetails",arguments: detailsBean);
                      Navigator.pushNamed(context, "/in_task_mterial_details",
                          arguments: entity.datas.taskDetailList);
                    },
                    title: "物资明细",
                    border: Border(
                        bottom: BorderSide(width: 1, color: Color(0xffdddddd)),
                        top: BorderSide(width: 1, color: Color(0xffdddddd))))
              ],
            )));
  }

  Future getData() async {
    var response = InternetAPI.getInstance().dio.get(
          path,
        );
    return response;
  }
}
